<template>
    <div>
        <a-modal
            :title="modalTitle"
            v-model="modalShow"
            @ok="onSubmit"
            :width="800"
            okText="确认"
            cancelText="取消"
        >
            <a-form :model="form" :label-col="labelCol" :wrapper-col="wrapperCol">
                <a-row>
                    <a-col span="12">
                        <a-form-item label="项目编码">
                            <a-input v-model="form.code" />
                        </a-form-item>
                    </a-col>
                    <a-col span="12">
                        <a-form-item label="项目名称">
                            <a-input v-model="form.name" />
                        </a-form-item>
                    </a-col>
                </a-row>
                <a-row>
                    <a-col span="12">
                        <a-form-item label="单位">
                            <a-select v-model="form.measureUnit">
                                <a-select-option
                                    v-for="p in unitList"
                                    :key="p.code"
                                    :value="p.name"
                                >{{ p.name }}</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                    <a-col span="12">
                        <a-form-item label="是否数值">
                            <a-select v-model="form.dataType">
                                <a-select-option
                                    v-for="p in dict.dataTypeOptions"
                                    :key="p.code"
                                    :value="p.value"
                                >{{ p.label }}</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                </a-row>
            </a-form>
        </a-modal>
    </div>
</template>

<script>
import dict from "../../../dict.json";
export default {
    data() {
        return {
            modalShow: false,
            modalTitle: "",
            action: "",
            labelCol: { span: 7 },
            wrapperCol: { span: 16 },
            dict,
            unitList: [],
            form: {},
        };
    },
    methods: {
        // 打开模态框
        openModal(data) {
            this.form = !!data ? data : {};
            this.modalTitle = !data ? "新增检验项目" : "编辑检验项目";
            this.action = !data ? dict.action.project.addAction : dict.action.project.editAction;
            this.modalShow = true;
            this.queryUnit();
        },
        queryUnit() {
            let self = this;
            self.$store
                .dispatch(dict.action.common.unitQueryAction)
                .then(res => {
                    if (parseInt(res.code) === 1) {
                        self.unitList = res.data;
                    }
                })
                .catch(err => {
                    self.$message.error("失败" + res.msg + "(" + res.code + ")");
                });
        },
        // 确定提交表单
        onSubmit() {
            let self = this;
            this.$store
                .dispatch(self.action, self.form)
                .then(res => {
                    if (parseInt(res.code) === 1) {
                        self.modalShow = false;
                        self.$message.success("成功");
                        self.$parent.init();
                    } else {
                        self.$message.error("失败" + res.msg + "(" + res.code + ")");
                    }
                })
                .catch(err => {
                    console.log(err);
                });
        },
    },
};
</script>
